package TUILayer;
import java.util.Scanner;

import ControlLayer.DVDCtr;
import ModelLayer.*;

/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dCtr;//DVD UI must know the DVD controller 
    private MainMenuUI mmu;
    
    public DvdUI()
    {
        mmu = new MainMenuUI();
        dCtr = new DVDCtr();
    }
    
    public void start()
    {
       dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if (choice == 1)
            {
                findDVD();
            }
            else
            {
                if (choice == 2)
                {
                   createDVD(this.inputDvdNo(), this.inputTitle(), this.inputArtist(), this.inputPublicationDate());
                }
                else
                {
                    if (choice == 3)
                    {
                        createCopy (this.inputDvdNo(), this.inputCopyNo(), this.purchaseDate(), this.price(), this.availability());
                    }
                    else
                    {
                        if (choice == 4)
                        {
                             deleteDVD(this.inputDvdNo());
                        }
                        else
                        {
                            if (choice == 5)
                            {
                                listAllDvds();
                            }
                            else
                            {
                                if (choice == 6)
                                {
                                        this.updateCopy();
                                    }
                                else
                                {
                                    if (choice==7)
                                    {
                                        this.deleteCopy();
                                    }
                                    else
                                    {
                                        if (choice==8)
                                        {
                                            //Return to main menu
                                            mmu.mainMenuStart();
                                        }
                                         else
                                        {
                                            exit = true;
                                        }//end else
                                    }//end else
                                }//end else
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }       
        }//end while
    }
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD description");
            System.out.println(" (3) Create Copy of the DVD");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Update Copy");
            System.out.println(" (7) Delete Copy");
            System.out.println(" (8) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            
            return choice;
    }
    private long inputDvdNo()
    {   
        boolean ok = false;
        long id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write DVD ID:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private String inputArtist()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD artist: ");
        String artist = keyboard.nextLine();
        
        return artist;
    }
    
    private String inputPublicationDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD publication date : ");
        String publicationDate = keyboard.nextLine();
        
        return publicationDate;
    }
        
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        
        return title;
    }
    
    private int inputCopyNo()
    {   
        boolean ok = false;
        int id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Copy Number:  ");
           try
           {
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    
    private String purchaseDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the purchase date of the copy: ");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }
    private double price()
    {   
        boolean ok = false;
        double id = 0;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Price:  ");
           try
           {
             id = keyboard.nextDouble();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    private boolean availability()
    {   
        boolean ok = false;
        boolean id = false;
        while (!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Availability of the Copy:  ");
           try
           {
             id = keyboard.nextBoolean();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter true/false -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        
        return id;
    }
    //there is need for more methods to read information about DVDs
    //and methods to display the information
    private void findDVD()
    {     
        long no = inputDvdNo();
        //is to be changed when the DVD controller is completed
        //DVD dvd = new DVD();
        //dvd = dCtr.getDvd(no);
        if (DVDCtr.getDvd(no) != null)
        {
            System.out.println("ID: " + DVDCtr.getDvd(no).getId());
            System.out.println("Title: " +DVDCtr.getDvd(no).getTitle());
            System.out.println("Artist: "+DVDCtr.getDvd(no).getArtist());
            System.out.println("Publication date: " +DVDCtr.getDvd(no).getPublicationDate());
        }
        else
            System.out.println("The DVD not exist");
        
    }
    
    private void createDVD(long _dvdId, String _title, String _artist, String _publicationDate)
    {
      if (DVDCtr.getDvd(_dvdId) == null)
      {
          DVD d = new DVD();
          d.setId(_dvdId);
          d.setTitle(_title);
          d.setArtist(_artist);
          d.setPublicationDate (_publicationDate);
          DVDCtr.createDvd(d);
          System.out.println("Successfully Added!");
      }
      else
      {
          System.out.println("This DVD already exists!");
      }
    }
    
    private void createCopy (long _dvdId, long _serialNumber, String _purchaseDate, double _price, boolean _availability)
    {
      if (DVDCtr.getDvd(_dvdId) != null)
      {
          if (DVDCtr.getCopy(_dvdId, _serialNumber) == null)
          {
              DVDCtr.createCopy(_dvdId, _serialNumber, _purchaseDate,  _price, _availability);
              System.out.println("Successfully Added!");
          }
          else
              System.out.println("This Copy already exists!");
      }
    }
    
    private void deleteDVD(long _dvdId)
    {
        if (DVDCtr.getDvd(_dvdId) != null) 
        {
            DVDCtr.deleteDvd(_dvdId);
            System.out.println("The DVD is deleted"); 
        }       
        else
            System.out.println("The DVD does not exist");                  
    }
    
    private void listAllDvds()
    {
        if (DVDCtr.listAllDvd() == null )
        {
            System.out.println("There are no DVDs in the system");
        }
        else
        {
            System.out.println("Results:");
            for (int i=0; i<DVDCtr.listAllDvd().size(); i++)
            {  
                System.out.println ("Title: " + DVDCtr.listAllDvd().get(i).getTitle() + " ID: " + DVDCtr.listAllDvd().get(i).getId());
                if (DVDCtr.getDvd(DVDCtr.listAllDvd().get(i).getId()).listAllCopies() == null)
                {
                    System.out.println("There are no Copies in the system for this DVD");
                }
                else
                {
                    for (int j=0; j<DVDCtr.getDvd(DVDCtr.listAllDvd().get(i).getId()).listAllCopies().size(); j++)
                    {
                        System.out.println("-Serial Number: " + DVDCtr.getDvd(DVDCtr.listAllDvd().get(i).getId()).listAllCopies().get(j).getSerialNumber() + " Availability: " + DVDCtr.getDvd(DVDCtr.listAllDvd().get(i).getId()).listAllCopies().get(j).getAvailability());
                    }
                }
            }
        }
    }
    
    private void updateCopy()
    {
        this.listAllDvds();
        DVDCtr.updateCopy(this.inputDvdNo(), new Copy(this.inputCopyNo(), this.purchaseDate(), this.price(), this.availability()));
    }
    
    private void deleteCopy()
    {
        this.listAllDvds();
        DVDCtr.deleteCopy(this.inputDvdNo(), this.inputCopyNo());
    }
}
